package com.ca.ui.core.adapter;

import android.annotation.SuppressLint;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.ca.ui.core.R;
import com.ca.ui.core.bean.MiniClubInfo;
import com.ca.ui.core.util.ImageUtils;
import com.ca.ui.core.util.PinYinUtils;
import com.ca.ui.core.view.bar.SortListBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class SortClubAdapter extends RecyclerView.Adapter<SortClubAdapter.SortClubHolder> {

    private final Map<String, Integer> map = new HashMap<>();

    private SortListBar bar;

    private List<MiniClubInfo> list;

    public SortClubAdapter(SortListBar bar, List<MiniClubInfo> list) {
        this.bar = bar;
        this.list = list == null ? new ArrayList<>() : list;
    }

    @NonNull
    @Override
    public SortClubHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sort_club_item_layout, parent, false);
        return new SortClubHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SortClubHolder holder, int position) {
        MiniClubInfo info = list.get(position);

        if (info == null) {
            return;
        }

        //  判断是否显示当前字母
        String s = String.valueOf(PinYinUtils.getPinyinInitials(info.getName()).charAt(0));
        if (Objects.requireNonNull(map.get(s)).equals(position)) {
            holder.currentView.setVisibility(View.VISIBLE);
            holder.currentView.setText(s);
        }
        else {
            holder.currentView.setVisibility(View.GONE);
        }

        //  设置图片和名称
        ImageUtils.loadRoundImage(holder.imageView.getContext(), info.getImageUrl(), holder.imageView);
        holder.textView.setText(info.getName());
        holder.cl.setOnClickListener(v -> {});
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @SuppressLint("NotifyDataSetChanged")
    public void setList(List<MiniClubInfo> list) {
        if (list == null) {
            list = new ArrayList<>();
        }

        //  按照名称首字母排序
        list.sort((o1, o2) -> {
            String s2 = PinYinUtils.getPinyinInitials(o2.getName());
            String s1 = PinYinUtils.getPinyinInitials(o1.getName());
            return s1.charAt(0) - s2.charAt(0);
        });

        if (!this.list.equals(list)) {

            this.list = list;

            //  设置最小字母
            if (bar != null) {
                int minCurrent = list.size() == 0 ? 0 : PinYinUtils.getPinyinInitials(list.get(0).getName()).charAt(0) - 'A';
                bar.setCurrentIndex(minCurrent);
            }

            //  设置Map，用于显示当前字母
            map.clear();
            for (int i = 0; i < list.size(); i++) {
                MiniClubInfo info = list.get(i);
                String s = String.valueOf(PinYinUtils.getPinyinInitials(info.getName()).charAt(0));
                if (map.get(s) == null) {
                    map.put(s, i);
                }
            }

            notifyDataSetChanged();
        }
    }

    public SortListBar getBar() {
        return bar;
    }

    public void setBar(SortListBar bar) {
        this.bar = bar;
    }

    public List<MiniClubInfo> getList() {
        return list;
    }

    public int getItemIndexByCurrent(String current) {
        Integer value = map.get(current);
        return value == null ? -1 : value;
    }

    public static class SortClubHolder extends RecyclerView.ViewHolder {

        public TextView currentView;
        public ConstraintLayout cl;
        public ImageView imageView;
        public TextView textView;

        public SortClubHolder(@NonNull View itemView) {
            super(itemView);

            currentView = itemView.findViewById(R.id.tv_current);
            cl = itemView.findViewById(R.id.cl);
            imageView = itemView.findViewById(R.id.iv);
            textView = itemView.findViewById(R.id.tv);
        }
    }
}
